<template>
	<view class="content" @tap.stop="$refs.uniselect.close()">
        <!-- navbar -->
        <u-navbar :fixed="true" :safeAreaInsetTop="true" :placeholder="true"
            @leftClick="back" title="人员列表">
        </u-navbar>
        <!-- select -->
        <view class="uniselect" :style="{top:systemInfo.headerH+'px'}">
            <uni-select ref="uniselect" bgColor="#fff" :selectData="selectData" @select="select"></uni-select>
        </view>
        <view style="height:74rpx"></view>


        <!-- 内容 -->
        <view class="uni-area">
            <!-- 列表 -->
            <uni-item-member-select class="u-m-t-30" 
                v-for="(tem,ind) in page.list" :key="ind" :listItem="tem" 
                @selectMember="selectMember">
            </uni-item-member-select>
            <!-- 返回顶部 -->
            <u-back-top :scroll-top="page.scrollTop" @tap="toTop"></u-back-top>
            <!-- 加载更多 -->
            <u-loadmore :status="page.status" />
        </view>

        <!-- 提示组件 -->
        <u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import {mapGetters,mapMutations} from 'vuex'
export default {
    data() {
        return {
            systemInfo: uni.getStorageSync('systemInfo'), // 设备信息
            type: 'clock',
            parameter:{
                deptId: uni.getStorageSync("userInfo").deptId, // 所属部门编号
                gcbh: uni.getStorageSync("userInfo").ssgcbh // 所属项目(工程)编号
            },
            selectData:[], // 下拉选择数据
            selectPrameter:{
                smrz:'', // 人员是否认证【非必传】 
                ryzt:'', // 人员状态【非必传】
            },
            leavePra:{ // 人员离场
                searchTerminal: 'PC',
                ywbh: null,
                gcbh: uni.getStorageSync("userInfo").ssgcbh,
                ywlx: 'salary_smzgl_ryxx',
                spzt: 0,
                ywzt: 0, 
                sfyx: 1, 
                ryzt:1
            }, 
            foremanPra:{ // 班组长变更
                id: null
            },
            enterPra:{ // 人员进场
                searchTerminal: 'PC',
                spzt: 0, // 审批状态
                ywzt: 0, // 业务状态
                ryztStr: '0,2', // 人员状态
                pqidStr: 'isNull', // 派遣编号
                szxywlx: 'salary_smzgl_pqxx_mx', // 所在业务类型
                ywlx: 'isNull' // 业务类型
            },
            // 分页相关参数
            paging:{
                pageNum: 1,
                pageSize: this.$config.pageSize,
            },
            page:{
                scrollTop: 0,
                status: 'nomore',
                total: 0,
                list: [], // 列表
            },
        }
    },

    computed: {
        // ...mapGetters(['']),
    },


    onPageScroll(e) {
        this.page.scrollTop = e.scrollTop;
    },
    onReachBottom(){
        if( this.page.list.length < this.page.total ){
            this.paging.pageNum ++
            this.getMemberList()
        }else{
            this.page.status = 'nomore'
        }
    },


    onLoad(option) {
        this.type = option.type
        if( option.type == 'leave' ) this.leavePra.ywbh = option.ywbh
        if( option.type == 'foreman' ) this.foremanPra.id = option.bzid
        // 人员是否认证
        this.$tools.staffRenzhengFn(this,res=>{
            this.selectData.splice(0,1,res)
            this.selectPrameter.smrz = res.value
            // 人员状态 
            this.$tools.staffStatusFn(this,res=>{
                this.selectData.splice(1,1,res)
                this.selectPrameter.ryzt = res.value
            })
        })
        // 获取人员列表
        this.getMemberList()
    },


    methods: {
        ...mapMutations(['setLeaveStaff','setSelectForeman','setEnterSteff']),

        // 返回
        back(){
            uni.navigateBack()
        },


        // 人员列表
        getMemberList(){
            this.page.status = 'loading'
            let api, parameter
            switch ( this.type ) {
                case 'clock':
                    api = this.$apis.MemberList
                    parameter = Object.assign(this.parameter,this.selectPrameter,this.paging)
                    break;
                case 'foreman':
                    api = this.$apis.GroupMembers
                    parameter = this.foremanPra
                    break;
                case 'leave':
                    api = this.$apis.memberLeaveMemberList
                    parameter = Object.assign(this.leavePra,this.selectPrameter,this.paging)
                    break;
                case 'enter':
                    api = this.$apis.enterStaffList
                    parameter = Object.assign(this.enterPra,this.selectPrameter,this.paging)
                    break;
            }
            this.$request(this,api,parameter).then(res=>{
                this.page.total = res.total
                const rows = res.data ? res.data : res.rows
                // console.log('人员列表',rows)
                if( res.total != 0 ){
                    this.page.status = 'loadmore'
                    rows.map((v,i)=>{
                        this.page.list.push(v)
                    })
                }else{
                    this.page.status = 'nomore'
                    uni.$u.toast('暂未查询到相关人员列表')
                }
            }).then()
        },
        // 选择人员
        selectMember(e){
            switch (this.type) {
                case 'clock':
                    if( e.data.bzid ){
                        uni.redirectTo({ url: '/pages/kaoqin-bag/member-clockin?bzbh='+e.data.bzid+'&rybh='+e.data.id })
                    }else{
                        uni.$u.toast('当前人员的班组Id为空')
                    }
                    break;
                case 'leave':
                    this.setLeaveStaff({
                        searchTerminal: "PC",
                        ywbh: this.leavePra.ywbh, // 业务编号
                        bzbh: e.data.bzid, // 班组编号
                        gz: e.data.zwzc, //	工种
                        sfbzz: e.data.sfzz==1?true:false, // 是否班组长
                        sfyx: e.data.sfyx, // 是否有效
                        lczt: e.data.lczt, // 离场状态
                        id: e.data.id, // 明细id
                        rybh: e.data.id, // 人员编号
                        xm: e.data.xm,
                        xb: e.data.xb,
                        zjhm: e.data.zjhm
                    })
                    uni.navigateBack()
                    break;
                case 'foreman':
                    this.setSelectForeman({
                        id: e.data.id, 
                        xm: e.data.xm,
                        xb: e.data.xb,
                        zjhm: e.data.zjhm,
                        lxfs: e.data.lxfs,
                        zjlx: e.data.zjlx
                    })
                    uni.navigateBack()
                    break;
                case 'enter':
                    this.setEnterSteff({
                        id: e.data.id
                    })
                    uni.navigateBack()
                    break;
                
            }
        },



        // 下拉选择
        select(e){
            this.selectPrameter[e.type] = e.value
            this.paging.pageNum = 1
            this.page.list = []
            this.getMemberList()
            this.toTop()
        },

        // 回到顶部
        toTop(){
            uni.pageScrollTo({ scrollTop: 0, duration: 300 })
        },
    }
}
</script>

<style scoped lang="scss">
    .content{
        .uniselect{
            width: 100%;
            position: fixed !important;
            left: 0;
        }
    }
</style>
